package me.work.pay.jsyl.app.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;

import java.util.ArrayList;
import java.util.List;

public class PlaySound {
    /**
     * 播放声音用的
     *
     * @param type biz_type为1时必存在，
     *             语音类型
     *             1：微信支付收款；
     *             2：支付宝支付收款；
     *             3：云闪付支付收款；
     *             4：储值卡支付收款；
     *             5：支付收款；
     */
    public void play(Context context, String num) {
        AssetManager am;
        try {
            am = context.getAssets();
            MediaPlayer player = new MediaPlayer();
            String sound = "";
            List<String> sounds = new ArrayList<>();

            if (".".equals(num)) {
                sound = "dian.mp3";
            } else {
                sound = "a"+num + ".mp3";
            }
            sounds.add(sound);
            AssetFileDescriptor afd = am.openFd(sounds.get(0));
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            player.setOnCompletionListener(mediaPlayer -> player.start());
            player.prepare();
            player.start();
            player.setOnCompletionListener(mp -> {
                player.stop();
            });

        } catch (Exception e) {
            String s = "";
        }

    }

}
